package com.atguigu.gulimall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.atguigu.common.valid.AddValid;
import com.atguigu.common.valid.UpdateStatusValid;
import com.atguigu.common.valid.UpdateValid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.atguigu.gulimall.product.entity.BrandEntity;
import com.atguigu.gulimall.product.service.BrandService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;


/**
 * 品牌
 *
 * @author xiaolin
 * @email 956704533@qq.com
 * @date 2020-06-08 22:10:40
 */
@RestController
@RequestMapping("product/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:brand:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = brandService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{brandId}")
    //@RequiresPermissions("product:brand:info")
    public R info(@PathVariable("brandId") Long brandId){
		BrandEntity brand = brandService.getById(brandId);

        return R.ok().put("brand", brand);
    }


    /**
     * 查询品牌信息列表
     */
    @RequestMapping("/infos")
    //@RequiresPermissions("product:brand:info")
    public R infos(@RequestParam("brandIds") List<Long> brandIds){
        List<BrandEntity> brands=brandService.getBrandsByIds(brandIds);
        return R.ok().put("brands", brands);
    }


    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:brand:save")
    public R save(@Validated(value = AddValid.class) @RequestBody BrandEntity brand /*BindingResult result*/){  //校验异常直接抛出，由SpringMVC捕获
//        if(result.hasErrors()){
//            Map<String,Object> map=new HashMap<>();
//            result.getFieldErrors().forEach(item -> {
//                String field = item.getField();
//                String message = item.getDefaultMessage();
//                map.put(field,message);
//            });
//            return R.error(400,"提交的数据不合法").put("data",map);
//        }else {
            brandService.save(brand);
//        }
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:brand:update")
    public R update(@Validated(value = UpdateValid.class) @RequestBody BrandEntity brand){
        //修改品牌信息时，品牌与分类关联表的冗余字段也要更新
        brandService.updateDetail(brand);
        //brandService.updateById(brand);

        //TODO 更新其他冗余字段

        return R.ok();
    }

    /**
     * 修改状态，修改状态单独抽出一个方法，不需要校验其他值
     */
    @RequestMapping("/update/status")
    //@RequiresPermissions("product:brand:update")
    public R updateStatus(@Validated(value = UpdateStatusValid.class) @RequestBody BrandEntity brand){
        brandService.updateById(brand);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:brand:delete")
    public R delete(@RequestBody Long[] brandIds){
		brandService.removeByIds(Arrays.asList(brandIds));

        return R.ok();
    }

}
